﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SCSService
{
    public class LanguageService
    {
        /// <summary>
        /// Get List Language
        /// </summary>
        /// <returns></returns>
        public List<System_Language> GetListLanguage()
        {
            using (var data = new DataDataContext())
            {
                var language = (from p in data.System_Languages
                                where p.LanguageStatus==1
                                select p).ToList();
                return language;
            }
        }

        public List<System_Language> GetListLanguageBySiteGuid(int siteGuid)
        {
            using (var data = new DataDataContext())
            {
                var list = (from l in data.System_Languages
                            join wl in data.System_Website_Languages on l.LanguagesID equals wl.LanguagesID into r0
                            from e0 in r0.DefaultIfEmpty()
                            join w in data.System_Websites on e0.SiteGuid equals w.SiteGuid into r1
                            from e1 in r1.DefaultIfEmpty()
                            where e1.SiteGuid == siteGuid && l.LanguageStatus==1
                            select l).ToList();

                return list;
            }
        }

        public System_Language GetLanguageByID(int id)
        {
            using (var data = new DataDataContext())
            {
                var language = (from p in data.System_Languages
                                where p.LanguagesID == id
                                select p).FirstOrDefault();
                return language;
            }
        }

        public int GetLanguageIDByCountryCode(string code)
        {
            using (var data = new DataDataContext())
            {
                var language = (from p in data.System_Languages
                                where p.countryCode == code
                                select p).FirstOrDefault();
                return language.LanguagesID;
            }
        }

        public List<System_Language_Tag> GetListTagBySiteGuidAndLanguageID(int languageID, int siteguid)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.System_Language_Tags
                            where p.LanguagesID == languageID && p.SiteGuid == siteguid
                            select p).ToList();
                return list;
            }
        }

        public System_Language_Tag GetTagBySiteGuidAndLanguageIDandTagName(int languageID, int siteguid, string TagName)
        {
            using (var data = new DataDataContext())
            {
                var tag = (from p in data.System_Language_Tags
                           where p.LanguagesID == languageID && p.SiteGuid == siteguid && p.TagName == TagName
                           select p).FirstOrDefault();
                return tag;
            }
        }

        public void CreateSystemWebsiteLanguage(System_Website_Language swl)
        {
            using (var data = new DataDataContext())
            {
                data.System_Website_Languages.InsertOnSubmit(swl);
                data.SubmitChanges();
            }
        }

        public List<System_Language_Tag> GetListTagBySiteGuid(int siteguid)
        {
            using (var data = new DataDataContext())
            {
                var list = (from p in data.System_Language_Tags
                            where p.SiteGuid == siteguid
                            select p).ToList();
                return list;
            }
        }

        public int CreateSystemLanguageTag(System_Language_Tag tag)
        {
            using (var data = new DataDataContext())
            {
                data.System_Language_Tags.InsertOnSubmit(tag);
                data.SubmitChanges();

                return tag.TagID;
            }
        }
    }
}
